///////////////////////////////////////////////////////////
//  ListPersistent.cpp
//  Implementation of the Class ListPersistent
//  Created on:      23-sep-2007 21:26:26
///////////////////////////////////////////////////////////

#include "ListPersistent.h"
#include <algorithm>
#include <iostream>

#ifndef ZERO
	#define ZERO 0
#endif


ListPersistent::ListPersistent()
	: atributte(""),
	size(0),
	newSortedAtributte("")
{

}

ListPersistent::ListPersistent(const ListPersistent& right)
	: atributte(right.atributte),
	size(right.size),
	newSortedAtributte(right.newSortedAtributte)
{
	lsPersistent = right.lsPersistent;
}

ListPersistent::~ListPersistent(){
	// Tendria que ver el tema de la memoria, pero se supone que cuando se carga aca la memoria fue tomada
	// asi que la libero
	if(this->lsPersistent.empty()==false)
	{	
		this->lsPersistent.clear();
	}
	if(this->lsPersistent.empty()==true)
	{	
		this->size=0;
	}
}

void ListPersistent::clear()
{
	for(std::vector<Persistent*>::iterator it = lsPersistent.begin(); it != lsPersistent.end(); it++)
	{
		if(*it != NULL)
		{	delete (*it);
			(*it)=NULL;
		}	
	}
	if(this->lsPersistent.empty()==false)
	{	
		this->lsPersistent.clear();
	}
	if(this->lsPersistent.empty()==true)
	{	
		this->size=0;
	}
}



std::string ListPersistent::getAtributte(){
	return this->atributte;	
}

void ListPersistent::setAtributte(std::string at){
	this->atributte = at;
}

void ListPersistent::insertSort(Persistent* object){
	bool flag = false;
	std::vector<Persistent*>::iterator current = lsPersistent.begin();

	if(object != NULL)
	{		
		while(current != lsPersistent.end() && !flag)
		{
			if((*current)->EsMayor(this->atributte, object))
			{
				lsPersistent.insert(current, object);
				flag = true;
			}
			current++;
		}
		if(!flag)
		{
			lsPersistent.push_back(object);
		}
		this->size++;
	}	
}

int ListPersistent::getSize(){
	return this->size;
}
Persistent* ListPersistent::getPersistent(int i)
{
	if (i < this->size)
	{
		
		return lsPersistent[i];
	}
	return NULL;
}

Persistent* ListPersistent::getFirst(){
	std::vector<Persistent*>::iterator it;
	
	if(this->lsPersistent.size() == ZERO)
		return NULL;
	it = this->lsPersistent.begin();
	return (*it);
}

Persistent* ListPersistent::getLast(){
	std::vector<Persistent*>::iterator it;
		
	if(this->lsPersistent.size() == ZERO)
		return NULL;
	it = this->lsPersistent.begin() + this->lsPersistent.size() - 1;
	return (*it);
}
int ListPersistent::erase(int i)
{	
	if(i < this->size)
	{
		if((this->lsPersistent[i]) != NULL)
		{
			delete (this->lsPersistent[i]);
			this->lsPersistent[i]=NULL;
		}
		lsPersistent.erase(lsPersistent.begin()+i);
		size = lsPersistent.size();
		return size;
	}
	return size;
}
bool ListPersistent::is(Persistent* object){
	int i = ZERO;
	
	for(i = ZERO; i < this->getSize(); i++)
	{
		if(this->getPersistent(i)->getKey() == object->getKey())
			return true;
	}
	return false;
}

int ListPersistent::indexOf(Persistent* object){
	bool find = false;
	int i = ZERO, retorno = -1;
	
	for(i = ZERO; i < this->getSize() && !find; i++)
	{
		if(this->getPersistent(i)->getKey() == object->getKey())
		{
			find = true;
			retorno = i;
		}
	}
	return retorno;
}

bool ListPersistent::Compare(Persistent *left, Persistent *right)
{
	std::string leftValue = left->getValor(this->newSortedAtributte);
	std::string rightValue = right->getValor(this->newSortedAtributte);
    if (NumberUtil::isNumber(leftValue)==1 && NumberUtil::isNumber(rightValue)==1){
    	double lValue = atof(leftValue.c_str());
    	double rValue = atof(rightValue.c_str());
    	return (lValue < rValue);
    }
	return (leftValue < rightValue);
}

bool ListPersistent::operator()(Persistent *left, Persistent *right)
{
	std::string leftValue = left->getValor(this->newSortedAtributte);
	std::string rightValue = right->getValor(this->newSortedAtributte);
	
    if (NumberUtil::isNumber(leftValue)==1 && NumberUtil::isNumber(rightValue)==1){
    	double lValue = atof(leftValue.c_str());
    	double rValue = atof(rightValue.c_str());
    	return (lValue < rValue);
    }
	return (leftValue < rightValue);
}

void ListPersistent::sort(std::string newAtributte)
{
	this->newSortedAtributte = newAtributte;
		
	std::sort(this->lsPersistent.begin(), this->lsPersistent.end(), (*this));
}

const ListPersistent & ListPersistent::operator=(const ListPersistent &right)
{
	if((this == &right))
	{
		return (*this);
	}
	
	if((right.size!= 0))
	{
		atributte = right.atributte;
		size = right.size;
		lsPersistent = right.lsPersistent;
		return (*this);
	}

	if((right.size== 0))
	{
		size = right.size;
		return (*this);
	}	
	return (*this);
}




